package com.coocaa.ops.admin.v1.hvi.service;

import com.coocaa.ops.admin.base.language.CommonLanguageProperties;
import com.coocaa.ops.admin.v1.hvi.biz.IHistoryVersionInfoBiz;
import com.coocaa.ops.admin.v1.hvi.model.HistoryQueryModel;
import com.coocaa.ops.admin.v1.hvi.model.HistoryVersionInfoEntity;
import com.coocaa.ops.admin.v1.hvi.provider.IHistoryInfoProvider;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.core.base.model.ResourceBaseModel;
import com.coocaa.ops.common.core.base.model.ResourceHistoryInfoModel;
import com.coocaa.ops.common.core.base.util.ResourceStatusValidator;
import com.coocaa.ops.common.tool.integer.IntegerUtil;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author bijiahao
 * @date : 2018/6/12.
 * @description
 */
public class HistoryVersionInfoServiceImpl implements IHistoryVersionInfoService {

    @Autowired
    private IHistoryVersionInfoBiz historyVersionInfoBiz;
    private static CommonLanguageProperties commonLanguageProperties;
    /**
     * 版本历史信息获取 提供者服务实例Map
     */
    @Setter
    private Map<String, IHistoryInfoProvider> historyInfoProviderMap;

    @Override
    public Object getHistoryList(HistoryQueryModel queryModel) {
        IHistoryInfoProvider historyInfoProvider = historyInfoProviderMap.get(queryModel.getType());
        Assert.notNull(historyInfoProvider, "The implementation class for the corresponding resource "+commonLanguageProperties.getNotNull()+"不能为空！type=" + queryModel.getType());
        int total = historyInfoProvider.getHistoryCount(queryModel.getId());
        List<ResourceHistoryInfoModel> historyInfoList = null;
        if (total > 0) {
            Integer currentPage = IntegerUtil.getInt(queryModel.getPage(), 1);
            Integer pageSize = IntegerUtil.getInt(queryModel.getRows(), 30);
            Integer offset = (currentPage - 1) * pageSize;
            historyInfoList = historyInfoProvider.getHistoryList(queryModel.getId(), pageSize, offset);
            setLaunchInfo(historyInfoList, queryModel.getId(), queryModel.getType(), pageSize+offset);
        }
        return R.ok(total, historyInfoList);
    }
    @Autowired
    public void setCommonLanguageProperties(CommonLanguageProperties commonLanguageProperties) {
        HistoryVersionInfoServiceImpl.commonLanguageProperties = commonLanguageProperties;
    }

    /**
     * 插入历史版本信息
     *
     * @param resourceBaseModel
     * @param userId
     */
    @Override
    public void addHistoryVersionInfo(ResourceBaseModel resourceBaseModel, Integer userId) {
        //无版本信息的不需要记录
        if (null != resourceBaseModel.getVersion()) {
            historyVersionInfoBiz.addHistoryInfo(
                    resourceBaseModel.getType(),
                    resourceBaseModel.getId(),
                    resourceBaseModel.getVersion(),
                    userId,
                    new Date());
        }
    }

    /**
     * 设置上线时间等信息
     *
     * @param historyList 结果集
     * @param id
     * @param type 资源类型
     */
    private void setLaunchInfo(List<ResourceHistoryInfoModel> historyList, Integer id, String type, Integer limit) {
        Map<Integer, HistoryVersionInfoEntity> map = historyVersionInfoBiz.getHistoryInfo(type, id, limit, null);
        if (historyList != null) {
            for (ResourceHistoryInfoModel model : historyList) {
                if (model != null) {
                    Integer version = model.getVersion();
                    if (map.containsKey(version)) {
                        HistoryVersionInfoEntity historyEntity = map.get(version);
                        model.setLaunchDate(historyEntity.getLaunchDate());
                        model.setModifierName(historyEntity.getLaunchUserName());
                    } else if (ResourceStatusValidator.isOnline(model.getStatus())
                            || ResourceStatusValidator.isOffline(model.getStatus())){
                        //对于遗留的没有上线时间的老数据，使用last_update_date填充
                        model.setLaunchDate(model.getLastUpdateDate());
                    }

                }
            }
        }
    }
}
